#-*- coding: utf-8 -*-
import db
from openpyxl import Workbook

def main():
  dbc = db.DbClass("test01")
  dbc.open()
  listTbl, dictTbl, dictCol, dictSts, dictCst = dbc.qryMeta()

#  print('--',dictTbl)
#  print('--',dictCol)
  print('--',dictSts)
  print('--',dictCst)

  wb = Workbook()
  wsi=wb.create_sheet('index', 0)
  lblSheet = ''
  tbl=''
  for i in range(len(listTbl)):
    tbl=listTbl[i]
    if dictTbl[tbl] == '':
      lblSheet = tbl
    else:
      lblSheet = dictTbl[tbl]

    wst=wb.create_sheet(lblSheet, i+1)
    wst.append(['colnm', 'ctype', 'nable', 'deflt', 'commt', 'colky', 'STS', 'CST'])
    for col in dictCol[tbl]:
      colnm=col['colnm']
      cols=[]
      cols.append(colnm)
      cols.append(col['ctype'])
      cols.append(col['nable'])
      cols.append(col['deflt'])
      cols.append(col['commt'])
      cols.append(col['colky'])

      if dictSts[tbl] != None:
        if colnm in dictSts[tbl]:
          cols.append('A')
        else:
          cols.append('B')

      if dictCst[tbl] != None:
        if colnm in dictCst[tbl]:
          cols.append('X')
        else:
          cols.append('Y')

      wst.append(cols)

  wb.save('tables.xlsx')
  wb.close()
  dbc.shutdown()

if __name__ == "__main__":
   main()

